﻿#define NODE_MATRIX __declspec(dllexport)

#include "NodeMatrix.h"

NodeMatrix::NodeMatrix() :
	level(1.0f),
	position(osg::Vec3d(0.0, 0.0, 0.0))
{
	pat = new osg::MatrixTransform;
	addChild(pat.get());
}

NodeMatrix::~NodeMatrix()
{

}

/**设置当前模型转动方式*/
void NodeMatrix::rotating(const osg::Vec3d& pivot, const osg::Vec3d& axis, float angularVelocity)
{ 
	setUpdateCallback(new osg::AnimationPathCallback(pivot, axis, angularVelocity));
}
/**旋转模型*/
void NodeMatrix::toRotate(const osg::Matrix& mat)
{
	pat->setMatrix(mat);
}

void NodeMatrix::toRotate(float angle, const osg::Vec3f& axis)
{
	pat->setMatrix(osg::Matrix::rotate(angle, axis));
}

/**缩放模型*/
void NodeMatrix::toScale(const osg::Matrix& mat)
{
	pat->setMatrix(mat);
}

void NodeMatrix::toScale(float lel)
{
	pat->setMatrix(osg::Matrix::scale(lel, lel, lel));
}

/**addsChild方法*/
void NodeMatrix::addsChild(osg::Node* node)
{
	pat->addChild(node);
	pnode = node;
	ps = node->getBound();
	osg::notify(osg::NOTICE) << ps.center().x() << " " << ps.center().y() << " " << ps.center().z() << std::endl;
}

void NodeMatrix::toPosition(const osg::Vec3d& pos)
{
	osg::Vec3d cps;
	cps.set(-ps.center().x() * level, -ps.center().y() * level, -ps.center().z() * level);	// adapt会缩放模型，所以这里要乘以level
	pat->setMatrix(osg::Matrix::translate(cps) * osg::Matrix::translate(pos));
	position = pos;
}

osg::Vec3d NodeMatrix::getPosition()
{
	return position;
}

void NodeMatrix::adapt(osg::BoundingSphere& sps)
{
	float level = sps.radius() / ps.radius();

	pat->setMatrix(osg::Matrix::scale(level, level, level));
}

void NodeMatrix::adapt(osg::Node* node)
{
	osg::BoundingSphere sps = node->getBound();
	level = sps.radius() / ps.radius();
	pat->setMatrix(osg::Matrix::scale(level, level, level));
}
